#!/bin/bash

# this is an example for demostrate how awk workflow works
# * the BEGIN block was executed at first part of execution sequence
#   it first set "field seperator" as ":", whitch is the result of statement FS=":",
#   then it prints the header.

# * then the Body block is executed. awk prints the first feild of a line 
#   only if the line contains word "mail", and due to the field seperator changing, awk 
#   treat ":" as seperator, not " ". awk execute continuly until reaches EOF of input.

# * after Body block finished, awk moves foots on END block, whitch contains only 1 work: 
#   print the foot notation, then awk exit.

awk 'BEGIN { FS=":"; print "-----HEADER-----"} \
     /mail/ {print $1} \
     END {print "-----FOOTER-----"}' \
     /etc/passwd

echo -e '\n'


# - Only use a BEGIN block
awk -F: 'BEGIN {print "hello world!"}'
echo -e '\n'

# if there is no Body and END block exists, the awk just exit after it finished the block in BEGIN 
# block
